﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_list : System.Web.UI.Page
{
    protected PagedDataSource pds = new PagedDataSource();
    protected string path = string.Empty;
    protected Topic parent;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["CurrentPage"] = 1;
            ViewState["PageCount"] = 1;

            #region 加载培训信息
            CmsDataContext db = new CmsDataContext();

            var ts = (from aT in db.Train
                      where (aT.Started - DateTime.Now).Days >= 0
                      orderby aT.Registed descending
                      select new
                      {
                          ID = aT.ID,
                          Memo = string.Format("{0}将于{1:yyyy-MM-dd HH:mm}在{2}举办", aT.Name, aT.Started, aT.Address),
                          Amended = aT.Amended != null
                      }).Take(6);
            Repeater2.DataSource = ts;
            Repeater2.DataBind();

            var px2 = (from aA in db.Article
                       where aA.Verified != null
                       orderby aA.Verified descending
                       select aA).Take(6);
            Repeater3.DataSource = px2;
            Repeater3.DataBind();
            #endregion

            BindData();
        }
    }

    private void BindData()
    {
        CmsDataContext db = new CmsDataContext();
        

        //
        //获取栏目id
        //
        string topic_id = Request.QueryString["topic_id"];
        parent = (from aT in db.Topic where aT.ID.Equals(topic_id) select aT).First();
        path = parent.Name;
        while (GetParent(parent.ID) != null)
        {
            parent = GetParent(parent.ID);
            path = parent.Name + "-->" + path;
        }
        path = "首页-->" + path;
        
        //
        //获取文章列表
        //
        var articles = from aA in db.Article
                       where aA.TopicID.Equals(topic_id) && aA.Verified != null
                       orderby aA.Published descending, aA.Subtitle
                       select aA;

        pds.DataSource = articles.ToList();
        pds.PageSize = 20;
        pds.AllowPaging = true;

        pds.CurrentPageIndex = (int)ViewState["CurrentPage"] >= pds.PageCount ? pds.PageCount - 1 : (int)ViewState["CurrentPage"] - 1;

        Repeater1.DataSource = pds;
        Repeater1.DataBind();

        ViewState["PageCount"] = pds.PageCount;
    }
    protected List<Topic> GetChild(string topic_id)
    { 
        CmsDataContext db=new CmsDataContext();
        var t = from aT in db.Topic where aT.ParentID.Equals(topic_id) select aT;
        if (t.Count() == 0)
        {
            return null;
        }
        else
        {
            return t.ToList<Topic>();
        }
    }
    protected string GetTopicID(string tName)
    {
        CmsDataContext db = new CmsDataContext();
        var t = from aT in db.Topic where aT.Name.Equals(tName) select aT;
        if (t.Count() != 0)
        {
            return t.First().ID;
        }
        else
        {
            return "";
        }
    }
    private Topic GetParent(string topic_id)
    {
        CmsDataContext db = new CmsDataContext();
        var t = from aT in db.Topic where aT.ID.Equals(topic_id) select aT;
        //var pt=from aT in db.Topic where aT.ParentID.Equals()
        if (t.First().ParentID!=null)
        {
            return t.First().Parent ;
        }
        else
        {
            return null;
        }
    }
    protected void ChangePage(object sender, ImageClickEventArgs e)
    {
        if (sender == FirstButton)
        {
            ViewState["CurrentPage"] = 1;
        }
        else if (sender == PreviousButton)
        {
            ViewState["CurrentPage"] = (int)ViewState["CurrentPage"] == 1 ? 1 : (int)ViewState["CurrentPage"] - 1;
        }
        else if (sender == NextButton)
        {
            ViewState["CurrentPage"] = (int)ViewState["CurrentPage"] == (int)ViewState["PageCount"] ? (int)ViewState["PageCount"] : (int)ViewState["CurrentPage"] + 1;
        }
        else if (sender == LastButton)
        {
            ViewState["CurrentPage"] = (int)ViewState["PageCount"];
        }

        BindData();
    }
}